package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.*;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.service.VehicleService;
import cn.edu.ujn.lab3.service.ParkAreaService;
import cn.edu.ujn.lab3.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 停车场管理控制器类
 */
@Controller
@RequestMapping("/parkArea")
public class ParkAreaController {

	@Autowired
	private BaseDictService baseDictService;
	@Autowired
    private ParkAreaService parkAreaService;
	private String AREA="070";
    private String TYPE="080";

	/**
	 * 车场区域列表
	 */
	@RequestMapping(value = "/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,
                       String area, String position, String type, String master,
			 Model model) {
		// 条件查询所有车场区域
		Page<ParkArea> parkAreaList = parkAreaService
				.finParkAreaList(page, rows, area,
                        position, type,master);
		model.addAttribute("page", parkAreaList);
		List<BaseDict> areas = baseDictService
				.findBaseDictByTypeCode(AREA);
        List<BaseDict> types = baseDictService
                .findBaseDictByTypeCode(TYPE);
        // 添加参数
        model.addAttribute("areas", areas);
		model.addAttribute("types", types);
		model.addAttribute("area", area);
		model.addAttribute("position", position);
		model.addAttribute("type", type);
        model.addAttribute("master", master);
		return "parkArea";
	}
	
	/**
	 * 创建停车场
	 */
	@RequestMapping("/create.action")
	@ResponseBody
	public String parkAreaCreate(ParkArea parkArea,HttpSession session) {
	    // 获取Session中的当前用户信息
	    User user = (User) session.getAttribute("USER_SESSION");
	    // 将当前用户id存储在停车场对象中
        parkArea.setUserId(user.getUser_id());
        parkArea.setUserName(user.getUser_name());

	    int rows = parkAreaService.createParkArea(parkArea);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	} 

	/**
	 * 通过id获取停车场信息
	 */
	@RequestMapping("/getParkAreaById.action")
	@ResponseBody
	public ParkArea getParkAreaById(Integer id) {
        ParkArea parkArea = parkAreaService.getParkAreaById(id);
	    return parkArea;
	}
	/**
	 * 更新停车场
	 */
	@RequestMapping("/update.action")
	@ResponseBody
	public String parkAreaUpdate(ParkArea parkArea) {
	    int rows = parkAreaService.updateParkArea(parkArea);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

	/**
	 * 删除停车场
	 */
	@RequestMapping("/delete.action")
	@ResponseBody
	public String parkAreaDelete(Integer id) {
	    int rows = parkAreaService.deleteParkArea(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

}

